'''
Description: 
Autor: didiplus
Date: 2023-01-13 11:28:44
LastEditors: lin
LastEditTime: 2023-01-13 11:44:03
'''


import threading
import time
from loguru import logger

lock = threading.Lock()

class Account:

    def __init__(self,balance) -> None:
        self.balance = balance


def draw_1(account:Account,amount):
    if account.balance >= amount:
        time.sleep(1)
        logger.info("{}取钱成功".format(threading.current_thread().name))
        account.balance -= amount
        logger.info("线程{}，{}余额".format(threading.current_thread().name,account.balance))
    else:
        logger.error("{}取钱失败，余额不足！".format(threading.current_thread().name))


def draw(account:Account,amount):
    with lock:
        if account.balance >= amount:
            time.sleep(1)
            logger.info("{}取钱成功".format(threading.current_thread().name))
            account.balance -= amount
            logger.info("线程{}，{}余额".format(threading.current_thread().name,account.balance))
        else:
            logger.error("{}取钱失败，余额不足！".format(threading.current_thread().name))        

if __name__ == "__main__":
    account = Account(1000)
    task1 = threading.Thread(target=draw,args=(account,800),name="task1")
    task2 = threading.Thread(target=draw,args=(account,800),name="taks2")

    task1.start()
    task2.start()